/**
 * 
 */
package com.wrsr.examsys.util;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;

import com.wrsr.examsys.domain.Resouce;

/**
 * ppt解析成图片
 * 
 * @author <a href="mailto:2397431783@qq.com">wangruishun</a>
 * @version examsy1.0 2015年1月5日 下午1:03:28
 */
public class PptToImage {
	public static int getImage(Resouce resouce) {
		List<String> list = new ArrayList<>();
		Slide[] slide = null;
		try {
			FileInputStream is = new FileInputStream(resouce.getPath());
			SlideShow ppt = new SlideShow(is);
			is.close();

			Dimension pgsize = ppt.getPageSize();

			slide = ppt.getSlides();
			for (int i = 0; i < slide.length; i++) {

				BufferedImage img = new BufferedImage(pgsize.width,
						pgsize.height, BufferedImage.TYPE_INT_RGB);
				Graphics2D graphics = img.createGraphics();
				// clear the drawing area
				graphics.setPaint(Color.white);
				graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,
						pgsize.height));

				// render
				slide[i].draw(graphics);

				// save the output
				FileOutputStream out = new FileOutputStream(new File(
						resouce.getImagePath() + "/" + (i + 1) + ".png"));

				list.add(resouce.getImagePath() + "/" + (i + 1) + ".png");
				javax.imageio.ImageIO.write(img, "png", out);
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return slide.length;
	}
}
